import { createOID, deepCopyObject, getSelectItem, hasOwnProp, travelTree } from '../utils/normalUtils'
import { queryDicListApi, querySystemListApi } from '../service/api/commonApi'
import { sendServiceData } from '../service'


/**
 * 系统背部容器 扩展
 */
export const APP_BACK_LAYER = 'app_back_layer_box_zc'

/**
 * 全部字典值
 */
export const LIST_ALL_VALUE = '-all'
/**
 * 全部字典对象
 */
export const LIST_ALL = { label: '全部', name: '全部', value: LIST_ALL_VALUE, id: LIST_ALL_VALUE }

/**
 * 生成全部对象
 * @param label
 * @constructor
 */
export const LIST_ALL_DATA = (label = '全部') => {
    return { label, name: label, value: LIST_ALL_VALUE, id: LIST_ALL_VALUE }
}

/**
 *  状态
 */
export const STATUS_LIST = [
    {
        label: '正常',
        value: '0'
    },
    {
        label: '停用',
        value: '1'
    }
]

/**
 * 创建带有全部的列表数据
 * @param list
 * @param allLabel
 */
export function createAllList(list:any[], allLabel = '全部') {
    const arr = [...list]
    arr.unshift(LIST_ALL_DATA(allLabel))
    return arr
}

/**
 * 系统列表
 */
export const DIC_LIST_TYPE = {
    SYSTEM: 'dic_system'
}

// ######################################################################### dic #######################################################

/**
 * 字典列表缓存
 */
const catchDicList:any = {}

/**
 * 字典请求 map
 */
const dicApiMap:any = {
    // [DIC_LIST_TYPE.SYSTEM]: querySystemListApi
}


/**
 * 字典表分类码值
 * @param type
 * @param all
 * @param stateProxy
 */
export async function queryDicList(type:string, all:any = '', stateProxy:any = null, params:any = {}) {
    if (!hasOwnProp(catchDicList, type)) {
        let apiMethod = queryDicListApi
        if (hasOwnProp(dicApiMap, type))apiMethod = dicApiMap[type]
        const result = await sendServiceData(apiMethod, { type, ...params }, stateProxy, true, true)
        const list = result || []
        for (const item of list) {
            item.label = item.name
            item.id = item.value
            item.key = item.value
        }
        catchDicList[type] = list
    }
    if (all) return [LIST_ALL_DATA(all === true ? '全部' : all), ...catchDicList[type]]
    return [...catchDicList[type]]
}

/**
 * 转换标准字典列表
 * @param arr
 * @param labelField
 * @param valueField
 */
export function transDicList(arr:any[], labelField = 'dicLabel', valueField = 'dicValue') {
    if (!arr) return []
    if (!labelField) {
        return arr.map((item:any) => {
            return { label: item, value: item }
        })
    }
   for (const item of arr) {
       item.label = item[labelField]
       item.value = item[valueField]
   }
   return arr
}

/**
 * 查询字典列表 并本地缓存
 * @param key
 * @param method
 * @param params
 * @param labelField
 * @param valueField
 * @param state
 */
export function queryCatchDicList(key:string, method:any, params:any, labelField = '', valueField = '', state:any = null) {
    if (hasOwnProp(catchDicList, key)) return catchDicList[key]
    const result:any = sendServiceData(method, params, state)
    catchDicList[key] = transDicList(result || [], labelField, valueField)
    return catchDicList[key]
}

